package cn.iocoder.yudao.module.famous.doctor.controller.admin.casehistory;

import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.certificate.FamCertificateDO;
import cn.iocoder.yudao.module.famous.doctor.service.certificate.FamCertificateService;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberDockerTjPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberDockerInfoRespVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.casehistory.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.casehistory.CaseHistoryDO;
import cn.iocoder.yudao.module.famous.doctor.convert.casehistory.CaseHistoryConvert;
import cn.iocoder.yudao.module.famous.doctor.service.casehistory.CaseHistoryService;

@Tag(name = "管理后台 - 我的病历")
@RestController
@RequestMapping("/famous.doctor/case-history")
@Validated
public class CaseHistoryController {

    @Resource
    private CaseHistoryService caseHistoryService;

    @Resource
    private FamCertificateService famCertificateService;
    @Resource
    private MemberUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建我的病历")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:create')")
    public CommonResult<Long> createCaseHistory(@Valid @RequestBody CaseHistoryCreateReqVO createReqVO) {
        return success(caseHistoryService.createCaseHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的病历")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:update')")
    public CommonResult<Boolean> updateCaseHistory(@Valid @RequestBody CaseHistoryUpdateReqVO updateReqVO) {
        caseHistoryService.updateCaseHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的病历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:delete')")
    public CommonResult<Boolean> deleteCaseHistory(@RequestParam("id") Long id) {
        caseHistoryService.deleteCaseHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的病历")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:query')")
    public CommonResult<CaseHistoryRespVO> getCaseHistory(@RequestParam("id") Long id) {
        CaseHistoryDO caseHistory = caseHistoryService.getCaseHistory(id);
        return success(CaseHistoryConvert.INSTANCE.convert(caseHistory));
    }

    @GetMapping("/list")
    @Operation(summary = "获得我的病历列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:query')")
    public CommonResult<List<CaseHistoryRespVO>> getCaseHistoryList(@RequestParam("ids") Collection<Long> ids) {
        List<CaseHistoryDO> list = caseHistoryService.getCaseHistoryList(ids);
        return success(CaseHistoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的病历分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:query')")
    public CommonResult<PageResult<CaseHistoryRespVO>> getCaseHistoryPage(@Valid CaseHistoryPageReqVO pageVO) {
        PageResult<CaseHistoryDO> pageResult = caseHistoryService.getCaseHistoryPage(pageVO);
        return success(CaseHistoryConvert.INSTANCE.convertPage(pageResult));
    }

    /**
     * 推荐医生
     * @param
     * @return
     */
    @GetMapping("/select-docker")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:select-docker')")
    @Operation(summary = "推荐医生", description = "推荐医生-- illnessIds疾病标签 有值:给患者推荐对应医生 空值:查询所有标签医生")
    public CommonResult<PageResult<AppMemberDockerInfoRespVO>> selectTjDocker(@Valid MemberDockerTjPageReqVO pageVO) {
        PageResult<MemberUserDO> user = userService.selectTjDocker(pageVO);
        PageResult<AppMemberDockerInfoRespVO> result = MemberUserConvert.INSTANCE.convertDocker(user);
        result.getList().forEach(appMemberDockerInfoRespVO -> {
            FamCertificateDO loginCertificate = famCertificateService.getLoginCertificate(appMemberDockerInfoRespVO.getId());
            if(loginCertificate!=null){
                appMemberDockerInfoRespVO.setDepartment(loginCertificate.getDepartment());
                appMemberDockerInfoRespVO.setHospitalName(loginCertificate.getHospitalName());
            }
        });

        return success(result);
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出我的病历 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:case-history:export')")
    @OperateLog(type = EXPORT)
    public void exportCaseHistoryExcel(@Valid CaseHistoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CaseHistoryDO> list = caseHistoryService.getCaseHistoryList(exportReqVO);
        // 导出 Excel
        List<CaseHistoryExcelVO> datas = CaseHistoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "我的病历.xls", "数据", CaseHistoryExcelVO.class, datas);
    }

}
